library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(gapminder)

Consider the bare minimum requirement

1. Let ggplot fuction knows: data + mapping

p <- ggplot(data = gapminder,   mapping = aes(x = gdpPercap, y = lifeExp))

2. choose your plot

p + geom_point()


See the reference on plot functions: Link. You need to play with some fuctions here!

Done. Class is over.

I am quite serious here. With some of variations, you can produce what you want roughly. From now on let’s add some details. Keep what you need. You have to make your own manual.

1. Build plots layer by layer

The process of adding layers to the plot really is additive, and it makes easier to assemble plots one piece at a time.

p <- ggplot(data = gapminder,
                    mapping = aes(x = gdpPercap, y = lifeExp)) 
p + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

What does geom_smooth() do? Write here based on your understanding.

p+ geom_smooth() + geom_point()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'


If we want to see the data points and the line together, we simply add geom_point() back in

Note that geom_smooth and geom_point are the functions. Two things we need to think about.

  1. Don’t have to tell geom_point() or geom_smooth() regarding data and aestitics

  2. They could accept inputs to give further instructions.

p + geom_smooth(method = "lm") + geom_point()
## `geom_smooth()` using formula 'y ~ x'

2. Transform the axis

Note that in the plot above, the value of GDP per capita is hihgly skewed. Let’s try to scale the value of x

Wait. What does mean scaling value of \(x\)? Why do we do?

p + geom_point() + geom_smooth(method = "gam") + scale_x_log10() 
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'


What does scale_x_log10() do? Is there any other scaling functions? Write here based on your understanding.

p + geom_point() + geom_smooth(method = "gam") + scale_x_log10(labels = scales::dollar) 
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'


Explain the argument in scale_x_log10(labels = scales::dollar)

3. More on aesthetics I

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = "pink"))
p + geom_point() +
    geom_smooth(method = "loess") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'

Look at legend. It is a bit suspicious. Let’s check out “green”.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = "green"))
p + geom_point() +
    geom_smooth(method = "loess") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'

Ok. Something wrong. Recall what the aes() does! Try now:

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = continent))
p + geom_point() +
    geom_smooth(method = "loess") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'

unique(gapminder$continent)
## [1] Asia     Europe   Africa   Americas Oceania 
## Levels: Africa Americas Asia Europe Oceania

Now, try to explain why the code with color = “green” produces the such pinkish graph.

One more thing.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = continent,
                          fill = continent))
p + geom_point() +
    geom_smooth(method = "loess") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'


What does fill argument do?

4. More on aesthetics II

There are five smoothers based on continent. Suppose I want to have a one trend smoother, but want to maintain color scatterplot by continent. aes() can be used geom_functions. Try this

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = continent)) +
    geom_smooth(method = "loess") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'


Mapping aesthetics on a per-geom basis. Here color is mapped to continent for the points but not the smoother.

Let’s review the two figures in words.

Continuous variables to the color aesthetic

What is a continous variable?

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = pop)) + scale_x_log10()    

p + geom_point(mapping = aes(color = log(pop))) + scale_x_log10()    

5. Figure size setting

Look at the first code chuck. It sets the default figure size.

knitr::opts_chunk$set(fig.width = 8, fig.height = 5)

If you want to chage the figure size without changing the default size

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = continent)) +
    geom_smooth(method = "loess") + scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'

6. How to save your figure?

ggsave("your_figure_name.png", plot = plot_object)
ggsave("your_figure.png", plot = p)
## Saving 8 x 5 in image

Well, the saved figure looks wrong. What’s wrong here?